using System;
using System.Text;
using AutoMapper;
using Hangfire;
using Hangfire.Dashboard;
using Hangfire.Redis;
using Hangfire.SqlServer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Sunny.Api.Filters;
using Sunny.Api.Interceptors;
using Sunny.AutoMapper;
using Sunny.Entity;
using Sunny.Hangfire;
using Sunny.Service;
using Sunny.Util.Exception;
using AuthorizationFilter = Sunny.Api.Filters.AuthorizationFilter;

namespace Sunny.Api
{
    public class Startup
    {
        private readonly IConfiguration _configuration;

        public Startup(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<SunnyContext>((serviceProvider,options)=>
            {
                var httpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
                var redisService = serviceProvider.GetRequiredService<RedisService>();
                options.AddInterceptors(new AutoFillInterceptor(httpContextAccessor,redisService));
                options.UseSqlServer(_configuration.GetConnectionString("SunnyConnection"))
                    .LogTo(Console.WriteLine,LogLevel.Information);
            });
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddScoped<AutoFillInterceptor>();
            
            services.AddScoped<RedisService>();
            services.AddScoped<UserService>();
            services.AddScoped<PermissionService>();
            services.AddScoped<MenuService>();
            services.AddScoped<DictTypeService>();
            services.AddScoped<DictDataService>();
            services.AddScoped<ConfigService>();
            services.AddScoped<DeptService>();
            services.AddScoped<RoleService>();
            services.AddScoped<PostService>();
            services.AddScoped<OperateLogService>();
            services.AddScoped<LoginLogService>();
            
            services.AddAutoMapper(config=>
            {
                config.AddProfile(typeof(UserProfile));
                config.AddProfile(typeof(SysDictProfile));
            });
            // services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
            
            services.AddControllersWithViews(config=>
            {
                config.Filters.Add<ExceptionHandler>();
                config.Filters.Add<LoginFilter>();
                config.Filters.Add<AuthorizationFilter>();
                config.Filters.Add<OperateFilter>();
                
            }).AddNewtonsoftJson(config =>
            {
                config.SerializerSettings.ContractResolver = new CustomContractResolver();
                config.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                config.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                config.SerializerSettings.Converters.Add(new IsoDateTimeConverter()
                    {DateTimeFormat = "yyyy-MM-dd HH:mm:ss"});
                config.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
            });
            services.AddCors(config=>
            {
                config.AddPolicy("cors",policy=>policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
            });
            //hangfire
            HangfireHelper.AddHangfire(services,_configuration);
        }



        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseCors("cors");
            
            HangfireHelper.UseHangfire(app);
            
            app.UseRouting();

            app.UseEndpoints(endpoints => { endpoints.MapDefaultControllerRoute(); endpoints.MapHangfireDashboard();});
        }
    }
}